﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Media3D;
using Eniux.Windows.ThreeDEngine.Helper;

namespace Eniux.Windows.ThreeDEngine.Visual3Ds
{
    /// <summary>
    /// 球体
    /// </summary>
    public class SphereVisual3D : MeshElement3D
    {
        #region 注册属性

        #region  Center
        /// <summary>
        /// 中心点属性
        /// </summary>
        public static readonly DependencyProperty CenterProperty = DependencyProperty.Register(
            "Center",
            typeof(Point3D),
            typeof(SphereVisual3D),
            new PropertyMetadata(new Point3D(0, 0, 0), GeometryChanged));

        /// <summary>
        /// 获取或设置中心点
        /// </summary>
        public Point3D Center
        {
            get{return (Point3D)GetValue(CenterProperty);}
            set{SetValue(CenterProperty, value);}
        } 
        #endregion


        #region PhiDiv
        /// <summary>
        /// PhiDiv 属性
        /// </summary>
        public static readonly DependencyProperty PhiDivProperty = DependencyProperty.Register(
            "PhiDiv", typeof(int), typeof(SphereVisual3D), new PropertyMetadata(30, GeometryChanged));

        /// <summary>
        /// 获取或者设置PhiDiv（从顶到底）
        /// </summary>
        public int PhiDiv
        {
            get{return (int)GetValue(PhiDivProperty);}
            set{SetValue(PhiDivProperty, value);}
        } 
        #endregion

        #region Radius
        /// <summary>
        /// 半径属性
        /// </summary>
        public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register(
            "Radius", typeof(double), typeof(SphereVisual3D), new PropertyMetadata(1.0, GeometryChanged));

        /// <summary>
        /// 获取或设置球体半径
        /// </summary>
        public double Radius
        {
            get { return (double)GetValue(RadiusProperty); }
            set { SetValue(RadiusProperty, value); }
        } 
        #endregion

        #region ThetaDiv
        /// <summary>
        /// ThetaDiv属性
        /// </summary>
        public static readonly DependencyProperty ThetaDivProperty = DependencyProperty.Register(
            "ThetaDiv", typeof(int), typeof(SphereVisual3D), new PropertyMetadata(60, GeometryChanged));

        /// <summary>
        /// 获取或设置ThetaDiv
        /// </summary>
        public int ThetaDiv
        {
            get { return (int)GetValue(ThetaDivProperty); }
            set { SetValue(ThetaDivProperty, value); }
        } 
        #endregion

        #endregion

        #region 方法

        /// <summary>
        /// Do the tesselation and return the <see cref="MeshGeometry3D"/>.
        /// </summary>
        /// <returns>A triangular mesh geometry.</returns>
        protected override MeshGeometry3D Tessellate()
        {
            var builder = new MeshBuilder(true, true);
            builder.AddSphere(this.Center, this.Radius, this.ThetaDiv, this.PhiDiv);
            return builder.ToMesh();
        }

        #endregion
    }
}
